package cn.itcast.usercontext.config;

import cn.itcast.usercontext.interceptor.ILoginUser;
import cn.itcast.usercontext.interceptor.UserContextInterceptor;
import cn.itcast.usercontext.interceptor.UserLoginInterceptor;
import cn.itcast.usercontext.interceptor.UserServiceLoginUser;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author sxt
 * @version 1.0
 * @description TODO
 * @date 2024/4/25 16:06
 */
@Configuration
public class DefaultUserContextWebMvcConfig implements WebMvcConfigurer {

    @Bean
    @ConditionalOnMissingBean(ILoginUser.class)
    public ILoginUser userServiceLoginUser() {
        return new UserServiceLoginUser();
    }

    @Bean
    HandlerInterceptor userContextInterceptor() {
        return new UserContextInterceptor();
    }

    // 添加用户上下文拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userContextInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new UserLoginInterceptor()).addPathPatterns("/**");
    }
}
